package services.api;

import java.util.*;

import dto.api.*;
import models.application.*;
import models.pubobj.*;
import services.security.*;
import utils.*;

public class ApiCallingService {

	//API calling
	public static String callApi(ApiAuthInfoDto apiAuthInfo, play.mvc.Http.Context context){
		Map<String, String> reqArgs = MVCUtils.getRequestArgs(context.request());
		return callApiWithAppRel(apiAuthInfo, reqArgs);
	}
	//-API authentication
	protected static String callApiWithAppRel(ApiAuthInfoDto apiAuthInfo, Map<String, String> params){
        AniApiAuthService.checkApiAuthInfoLegality(apiAuthInfo);
        AniApiAppRel apiAppRel = AniApiAppRel.withClientAppIdTargetApiId(
                apiAuthInfo.clientAppId,
                apiAuthInfo.targetApiId
        );
		AniApiAuthService.checkApiRequiredParams(apiAppRel.targetApi, params);
		return callApi(apiAppRel);
	}
	//-call url
	protected static String callApi(AniApiAppRel apiAppRel){
		String targetUrl = apiAppRel.targetApi.url;
		AniHttpMethod httpMethod = apiAppRel.targetApi.method;
		HashMap<String, String> params = AniApiParam.paramsToMap(apiAppRel.targetApi.params);
		
		return HttpUtils.callUrlSync(targetUrl, httpMethod.httpMethodKey, params);
	}
}
